using System;
using System.Collections.Generic;
using System.Text;

using SSRSDeployer.Properties;

namespace SSRSDeployer
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length <= 0)
            {
                WriteUsage();
                return;
            }

            ReportingServiceDeployer deployer = null;
            try
            {
                Dictionary<string, string> arguments = GetArguments(args);

                if (arguments.ContainsKey("/irpt:"))
                    deployer = new ReportDeployer(arguments);
                else if (arguments.ContainsKey("/idsc:"))
                    deployer = new DatasourceDeployer(arguments);
                else if (arguments.ContainsKey("/ifld:"))
                    deployer = new FolderDeployer(arguments);
                else if (arguments.ContainsKey("/irsc:"))
                    deployer = new ResourceDeployer(arguments);
                else
                    WriteUsage();

                deployer.Deploy();
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        private static Dictionary<string, string> GetArguments(string[] args)
        {
            Dictionary<string, string> result = new Dictionary<string, string>();
            for(int i = 0; i < args.Length; i++)
            {
                string arg = args[i];
                if (!arg[0].Equals('/'))
                    throw new ArgumentException(string.Format(Resources.ArgumentMustStartWithSlash, arg));
                int endIndex = arg.IndexOf(':');
                if (endIndex < 0)
                    throw new ArgumentException(string.Format(Resources.ArgumentMustEndWithSemiColumn, arg));
                endIndex++;
                string key = arg.Substring(0, endIndex);
                string waarde = arg.Substring(endIndex);
                if (string.IsNullOrEmpty(waarde))
                    throw new ArgumentException(string.Format(Resources.ArgumentIsNullOrEmpty, key));

                result.Add(key.ToLowerInvariant(), waarde);
            }
            return result;
        }

        private static void WriteUsage()
        {
            Console.WriteLine(Resources.UsageParent);
            Console.WriteLine(string.Empty);
            Console.WriteLine(Resources.UsageReportDefinition);
            Console.WriteLine(Resources.UsageDatasource);
            Console.WriteLine(Resources.UsageFolder);
            return;
        }
    }
}
